<?php
require '../config.php';
require PATH . 'classes/ofj-orm/Usuario.php';

Lang::setSource('seguidores');

$ResponseData = (object)array(
  'error' => false
);

// Limpiar datos
$_POST = array_map('trim', $_POST);

// Verificar datos de envio
if($_POST['name'] == '' || $_POST['email'] == '' || $_POST['celphone'] == ''
  || $_POST['city'] == ''){
  $ResponseData->error = Lang::get('msg_fill_form');
} else {

  try{
    $count = Usuario::instance()->count(array(
      'where'     => 'email=:email',
      'arguments' => array(':email' => $_POST['email'])
    ));

    if($count == 0){
      $Usuario = new Usuario();
      $Usuario->nombre     = $_POST['name'];
      $Usuario->email      = $_POST['email'];
      $Usuario->celular    = $_POST['celphone'];
      $Usuario->localidad  = $_POST['city'];
      $Usuario->frecuencia = $_POST['frecuencia'];
      $Usuario->fecha = new SahORMSqlExpression('NOW()');
      $Usuario->save();
    } // END: if($count == 0)
  }catch(SahORMException $e){
    $ResponseData->error = Lang::get('msg_db_error');
  }// END: try-catch
} // END: if($_POST['name'] == '' ... )

header('Content-Type:application/json;charset=utf-8');
echo json_encode($ResponseData);